package com.gxa.learning.pc.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.pojo.ProjectDetails;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.mapper.ProjectDetailsMapper;
import com.gxa.learning.pc.service.ProjectDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ProjectDetailsServiceImpl extends BaseServiceImpl<ProjectDetails> implements ProjectDetailsService {
    @Autowired
    private ProjectDetailsMapper projectDetailsMapper = null;

    @Autowired
    private Mapper<Project> projectMapper = null;


    @Override
    public Integer addDetailsAndProject(ProjectDetails projectDetails) throws BusinessException {
        if (projectDetails == null || projectDetails.getProject() == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        Project project = projectDetails.getProject();
//        project.setProjectStatus(0);
        int insert = projectMapper.insert(project);
        projectDetails.setProjectId(project.getProjectId());
        System.out.println(project);
        Integer integer = projectDetailsMapper.addProjectAndDetails(projectDetails);

        if (insert < 1) {
            throw new BusinessException(ExceptionCode.FAILED_OPTION);
        }
        if (integer < 1) {
            throw new BusinessException(ExceptionCode.FAILED_OPTION);
        }
        return integer;
    }

    @Override
    public void updateDetailsAndProject(ProjectDetails projectDetails) throws BusinessException {
        if (projectDetails.getDetailsId() == null || projectDetails.getDetailsId() <= 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        ProjectDetails thisDetails = projectDetailsMapper.selectByPrimaryKey(projectDetails.getDetailsId());
        if (thisDetails == null) {
            throw new BusinessException(ExceptionCode.NO_SUCH_OBJ);
        }
        Long projectId = thisDetails.getProjectId();
        if (projectId == null) {
            throw new BusinessException(ExceptionCode.WRONG_PROJECT);
        }
        int i = projectDetailsMapper.updateByPrimaryKeySelective(projectDetails);
        if (i < 1) {
            throw new BusinessException(ExceptionCode.INVALID_OPERATION);
        }
        Project thisProject = projectMapper.selectByPrimaryKey(projectId);
        Project project = projectDetails.getProject();
        project.setProjectId(thisProject.getProjectId());
        int k = projectMapper.updateByPrimaryKeySelective(project);
        if (k < 1) {
            throw new BusinessException(ExceptionCode.INVALID_OPERATION);
        }
    }

    @Override
    public ProjectDetails getProjectDetails(Long pid) throws BusinessException {
        if (pid == null || pid <= 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        ProjectDetails projectDetails = projectDetailsMapper.selectById(pid);
        if (projectDetails == null) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        return projectDetails;
    }

    @Override
    public List<Map<String, Object>> getTypeBy(String status) throws BusinessException {
        if (StringUtil.isEmpty(status)) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        List<Map<String, Object>> data = projectDetailsMapper.getTypeAndStatus(status);
        return data;
    }

    @Override
    public List<Map<String, Object>> getCertificates() {
        return projectDetailsMapper.getCertificates();
    }

    @Override
    public List<ProjectDetails> getByType(Long tid) throws BusinessException {
        if (tid == null || tid <= 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        return projectDetailsMapper.getByType(tid);
    }

    @Override
    public ProjectDetails getById(Long id) throws BusinessException {
        if (id == null || id <= 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        //
        return projectDetailsMapper.getById(id);
    }
}
